package com.dream.service.recommender.hub.util;

import com.dream.service.recommender.domain.model.proto.ProductListProto;
import com.dream.service.recommender.toolkits.cache.RedisCache;
import com.dream.service.recommender.toolkits.utils.StringUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.*;

/**
 * Created by zhiji on 2016/4/26.
 */
public class CacheUtil {

    public static ProductListProto.ProductListPro getCache(RedisCache redisCache, String key){
        byte[] values = redisCache.getFromCache(key.getBytes());
        if (values == null) return null;
        try {
            ProductListProto.ProductListPro productListPro = ProductListProto.ProductListPro.parseFrom(values);
            return productListPro;
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Set<Long> getExistSpus(RedisCache redisCache, String key, int size){
        ProductListProto.ProductListPro productListPro= getCache(redisCache, key);
        if(productListPro == null){
            return null;
        }
        Set<Long> result = new HashSet<Long>();
        List<Long> spus = productListPro.getSpusList();
        if (size < 0 || spus.size() < size) {
            result.addAll(spus);
        } else {
            result.addAll(spus.subList(0, size));
        }
        return result;
    }

    public static Set<Long> getImprSpus(RedisCache redisCache, String key){
        ProductListProto.ProductListPro productListPro= getCache(redisCache, key);
        if(productListPro == null){
            return null;
        }
        Set<Long> result = new HashSet<Long>();
        List<Long> spus = productListPro.getSpusList();
        result.addAll(spus);

        return result;
    }



    public static List<Long> backupCache(RedisCache redisCache, String key, Set<Long> spus, int expire, int subSize){
        if (StringUtils.isEmpty(spus)){
            return null;
        }
        ProductListProto.ProductListPro productListPro= getCache(redisCache, key);
        ProductListProto.ProductListPro.Builder builder = null;
        ProductListProto.ProductListPro.Builder productListBuilder = null;
        if(productListPro == null){
            productListBuilder =  ProductListProto.ProductListPro.newBuilder();
            builder = ProductListProto.ProductListPro.newBuilder();
        }else{
            productListBuilder = productListPro.toBuilder();
            builder = ProductListProto.ProductListPro.newBuilder();
        }

        try {
            builder.setKey(key);
            builder.setUpdateTime(new Date().getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Long> existSpus = productListBuilder.getSpusList();
        Set<Long> ids = new HashSet<Long>();
        if (subSize <= 0) {
            for(Long spu : existSpus){
                if (ids.add(spu)) {
                    builder.addSpus(spu);
                }
            }
        } else {
            for (int i = 0; i < subSize && i < existSpus.size(); i++){
                Long spu = existSpus.get(i);
                if (ids.add(spu)) {
                    builder.addSpus(spu);
                }
            }
        }
        Iterator<Long> iterator = spus.iterator();
        while (iterator.hasNext()) {
            Long spu = iterator.next();
            if(ids.add(spu)) {
                builder.addSpus(spu);
            }
        }
        redisCache.setToCache(key.getBytes(), builder.build().toByteArray(), expire);
        return builder.getSpusList();
    }
}
